package handlers;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;

import GUI.*;

import javax.swing.*;

public class WindowManager {
	MainFrame mainWin;
	Login loginWin;
	SectionManager sectionMng;
	Color selColour;
	ColourSchema colours;
	
	public WindowManager() {
		mainWin = new MainFrame();
		loginWin = new Login();
		sectionMng = new SectionManager(mainWin);
		colours = new ColourSchema();
		selColour = colours.getSelected();
	}
	
	public void start() {
		loginWin.setVisible(true);
	}
	
	public void login(String usr, char[] pass) {
		if(verifyUser(usr, pass)) {
			close("login");
			mainWin.setVisible(true);
		}
		else {
			javax.swing.JOptionPane.showMessageDialog(loginWin,
	                "Enter your credentials right, you're offending Cthulhu",
	                "Haru says:",
	                javax.swing.JOptionPane.ERROR_MESSAGE);
		}
	}
	public void logout() {
		close("main");
		loginWin.setVisible(true);
	}
	
	//TODO Win Functions
	public void close(String w) {
		if (w.equals("login")) {
			WindowEvent wev = new WindowEvent(loginWin, WindowEvent.WINDOW_CLOSING);
		    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
		    loginWin.setVisible(false);
		    loginWin.dispose();
		}
		else if (w.equals("main")) {
			WindowEvent wev = new WindowEvent(mainWin, WindowEvent.WINDOW_CLOSING);
		    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
		    mainWin.setVisible(false);
		    mainWin.dispose();
		}
	}
	
	private boolean verifyUser(String usr, char[] pass) {
		//TODO extraer datos de la BD && enable pass validation
		boolean validUsr = false;
		
		switch (usr) {
		case "caja":
			validUsr = true;
			shapeWindow("caja");
			break;
		case "super_caja":
			validUsr = true;
			shapeWindow("super_caja");
			break;
		case "bodega":
			validUsr = true;
			shapeWindow("bodega");
			break;
		case "ventas":
			validUsr = true;
			shapeWindow("ventas");
			break;
		case "cfo":
			validUsr = true;
			shapeWindow("cfo");
			break;
		case "rh":
			validUsr = true;
			shapeWindow("rh");
			break;
		case "admin":
			validUsr = true;
			shapeWindow("admin");
			break;
		}
		
		String p = "horchata";
		return validUsr/* && verifyPassword(pass, p.toCharArray())*/;
	}
	
	private boolean verifyPassword(char[] input, char[] realPass) {
		if (input.length != realPass.length) {
			return false;
		}
		else {
			int i = 0;
			
			for(i=0; i<input.length && input[i]==realPass[i]; ++i);
						
			return i == realPass.length;
		}		
	}
	
	private void shapeWindow(String usr) {
		//TODO agregar y quitar botones dependiendo del usuario
		JMenu[] menus = mainWin.getToggleMenu(usr);
		switch (usr) {
		case "caja":
			mainWin.setUserRole("Cajero");
			menus[0].setVisible(false);
			menus[1].setVisible(false);
			
			enableVentas(1);
			enableInventario(2);
			clearBtns(3,4,5,6,7);
			break;
		case "super_caja":
			mainWin.setUserRole("Jefe de Caja");
			menus[1].setVisible(false);
			
			enableVentas(1);
			enableInventario(2);
			enableUsuarios(3);
			enableClientes(4);
			clearBtns(5,6,7);
			break;
		case "bodega":
			mainWin.setUserRole("Bodega");
			menus[0].setVisible(false);
			menus[1].setVisible(false);
			
			enableInventario(1);
			clearBtns(2,3,4,5,6,7);
			break;
		case "ventas":
			mainWin.setUserRole("Ventas");
			menus[0].setVisible(false);
			
			enableInventario(1);
			enableProveedores(2);
			clearBtns(3,4,5,6,7);
			break;
		case "cfo":
			mainWin.setUserRole("Chief Financial Officer");
			enableInventario(1);
			enableClientes(2);
			enableProveedores(3);
			enableRetail(4);
			enableBodegas(5);
			clearBtns(6,7);
			break;
		case "rh":
			mainWin.setUserRole("Recursos Humanos");
			menus[0].setVisible(false);
			menus[1].setVisible(false);
			
			enableUsuarios(1);
			clearBtns(2,3,4,5,6,7);
			break;
		case "admin":
			mainWin.setUserRole("Jefe Apache Haru");
			enableVentas(1);
			enableInventario(2);
			enableClientes(3);
			enableProveedores(4);
			enableRetail(5);
			enableBodegas(6);
			enableUsuarios(7);
			break;
		}
	}
	
	//TODO modules
	private void enableVentas(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Ventas");
		btn.setVisible(true);
		//Header
		if (bPos == 1) {
			mainWin.setCurrentHeader("ventas");
			btn.setEnabled(false);
			btn.setForeground(selColour);
		}
	}
	private void enableInventario(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Inventario");
		btn.setVisible(true);
		if (bPos == 1) {
			mainWin.setCurrentHeader("inventario");
			btn.setEnabled(false);
			btn.setForeground(selColour);
		}
	}
	private void enableClientes(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Clientes");
		btn.setVisible(true);
		if (bPos == 1) {
			mainWin.setCurrentHeader("clientes");
			btn.setEnabled(false);
			btn.setForeground(selColour);
		}
	}
	private void enableProveedores(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Proveedores");
		btn.setVisible(true);
		if (bPos == 1) {
			mainWin.setCurrentHeader("proveedores");
		}
	}
	private void enableRetail(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Tiendas");
		btn.setVisible(true);
		if (bPos == 1) {
			mainWin.setCurrentHeader("tiendas");
			btn.setEnabled(false);
			btn.setForeground(selColour);
		}
	}
	private void enableBodegas(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Bodegas");
		btn.setVisible(true);
		if (bPos == 1) {
			mainWin.setCurrentHeader("bodegas");
		}
	}
	private void enableUsuarios(int bPos) {
		//Button
		JButton btn = mainWin.getBtnX(bPos);
		btn.setText("Usuarios");
		btn.setVisible(true);
		if (bPos == 1) {
			mainWin.setCurrentHeader("usuarios");
			btn.setEnabled(false);
			btn.setForeground(selColour);
		}
	}

	//TODO Clearer
	private void clearBtns(int ... btns) {
		for(int btn : btns) {
			mainWin.getBtnX(btn).setVisible(false);
		}
	}
}